package leetcode;

/**
 * @Project : AlgorithmLearning
 * @Package : Leetcode
 * @File : Q0048.java
 * @Author : WangRuoyu
 * @Date 2023/11/18 21:26
 */


public class Q0048 {
    public void rotate(int[][] matrix) {
        final int N = matrix.length;
        for (int i = 0; i < N >> 1; i++) {
            for (int j = 0; j < N >> 1; j++) {
                int x = i;
                int y = j;
                int val = matrix[x][y];
                for (int k = 0; k < 4; k++) {
                    int tmp = matrix[y][N - 1 - x];
                    matrix[y][N - 1 - x] = val;
                    val = tmp;
                    int t = x;
                    x = y;
                    y = N - 1 - t;
                }
            }
        }
        if (N % 2 == 1) {
            for (int i = 0; i < N >> 1; i++) {
                int x = i;
                int y = N >> 1;
                int val = matrix[x][y];
                for (int k = 0; k < 4; k++) {
                    int tmp = matrix[y][N - 1 - x];
                    matrix[y][N - 1 - x] = val;
                    val = tmp;
                    int t = x;
                    x = y;
                    y = N - 1 - t;
                }
            }
        }
    }

    public static void main(String[] args) {
        Q0048 sl = new Q0048();
        int[][] nums = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        sl.rotate(nums);
    }
}